/**
 * Filename: NativeAlbumFragment.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/10 1:11
 */
package com.github.postsanf.yinian.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.pop.PopListImageDir;
import com.github.postsanf.yinian.adapter.LocalStickyImportPregressGridAdapter;
import com.github.postsanf.yinian.bean.ShowBackup;
import com.github.postsanf.yinian.bean.YNImageFloder;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.utils.YNNativeComparator;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * 本地图库
 */
public class ImportProgressFragment extends BaseFragment implements ISkinUpdate {
    private View view;                           // 上下文视图
    private TextView btn_native_up_choose;
    private GridView mNative_grid_view;
    private Map<String, Integer> sectionMap = new HashMap<>();
    private List<YNNativeImage> mNativeImageList = new ArrayList<>();
    private static int section = 1;
    private boolean isChoose = false;
    private LocalStickyImportPregressGridAdapter mAdapter;
    private PopListImageDir mListImageDirPopupWindow;
    private int mScreenHeight;                    // 屏高
    private RelativeLayout rl_native_up_bottom;
    // 所有的图片文件夹
    private List<YNImageFloder> mImageFloders = new ArrayList<YNImageFloder>();
    private ImageView iv_btn_dir_choose;

    private List<String> mImgs;                            // 所有的图片
    private Resources mResource;

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            // 为View绑定数据
        }
    };
    // 数据绑定
    private void data2View() {
        ShowBackup showBackup = new ShowBackup();
        List<ShowBackup.DataBean> data = showBackup.getData();
        for (int i = 0; i <data.size() ; i++) {
            String backupDate = data.get(i).getBackupDate();
            List<ShowBackup.DataBean.BackupPhotoBean> backupPhoto = data.get(i).getBackupPhoto();
            for (int j = 0; j <backupPhoto.size() ; j++) {
                ShowBackup.DataBean.BackupPhotoBean backupPhotoBean = backupPhoto.get(j);
                String backupPhotoURL = backupPhotoBean.getBackupPhotoURL();
                YNNativeImage mNativeItem = new YNNativeImage(backupPhotoURL, backupDate);
                mNativeImageList.add(mNativeItem);
            }
        }
        Collections.sort(mNativeImageList, new YNNativeComparator());
        for (ListIterator<YNNativeImage> it = mNativeImageList.listIterator(); it.hasNext(); ) {
            YNNativeImage mNativeItem = it.next();
            String ym = mNativeItem.getTime();
            if (!sectionMap.containsKey(ym)) {
                mNativeItem.setSection(section);
                sectionMap.put(ym, section);
                section++;
            } else {
                mNativeItem.setSection(sectionMap.get(ym));
            }
        }
        mAdapter = new LocalStickyImportPregressGridAdapter(getActivity(), mNativeImageList, true);
        if (mResource != null) {
            mAdapter.setResource(mResource);
        }
        mNative_grid_view.setAdapter(mAdapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        BaseApplication.getInstance().mActivitys.add(this);
        mScreenHeight = DisplayUtils.getScreenHeightPixels(getActivity());
        initView();
        data2View();
        return view;
    }

    private void initView() {
        view = View.inflate(getActivity(), R.layout.yn_fragment_progress_album, null);

        mNative_grid_view = (GridView) view.findViewById(R.id.native_grid_view);
        rl_native_up_bottom = (RelativeLayout) view.findViewById(R.id.rl_native_up_bottom);
        btn_native_up_choose = (TextView) view.findViewById(R.id.btn_native_up_choose);
        iv_btn_dir_choose = ((ImageView) view.findViewById(R.id.iv_btn_dir_choose));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    @Override
    public void updateTheme() {
        try {
            Resources mResource = SkinPackageManager.getInstance(getActivity()).mResources;
            btn_native_up_choose.setTextColor(mResource.getColor(mResource.getIdentifier("border_color", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
            rl_native_up_bottom.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
            mAdapter.setResource(mResource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(getActivity()).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart("MainScreen");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }
}